package com.quartz.example;


import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

import static org.quartz.CronScheduleBuilder.cronSchedule;

public class CronTriggerOne implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("吊炸天:"+new Date());
    }
    public Trigger cron(JobDetail jobDetail){
        //使用 cron表达式
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("sister")
                .forJob(jobDetail)
                .withSchedule(cronSchedule("0/3 * * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 组合表达式
     * @param jobDetail
     * @return
     */
    public Trigger combinationExpress(JobDetail jobDetail){
        //来自CronScheDuleBuilder
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail)
                .withIdentity("brother")
                .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(10, 42))
                .build();
        return trigger;
    }

    public static void main(String[] args) throws Exception{
        CronTriggerOne cronTriggerOne = new CronTriggerOne();
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        JobDetail jobDetail = JobBuilder.newJob(CronTriggerOne.class).build();

        Trigger trigger = cronTriggerOne.cron(jobDetail);;
        //使用代码
        scheduler.scheduleJob(jobDetail,trigger);
        scheduler.start();
//        scheduler.shutdown();

    }
}
